﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PublicTransportEnabler.net.Model
{
    public  class Trip : Part
	{
		private static  long serialVersionUID = 1312066446239817422L;

		public  Line Line { get; private set; }
		public  Location Destination { get; private set; }
		public  Stop DepartureStop { get; private set; }
		public  Stop ArrivalStop { get; private set; }
		public  List<Stop> IntermediateStops { get; private set; }
		public  string Message { get; private set; }

        public Trip(Line line, Location destination, Stop departureStop, Stop arrivalStop,
            List<Stop> intermediateStops, List<Point> path, string message)
            : base(
                departureStop != null ? departureStop.Location : null, arrivalStop != null ? arrivalStop.Location : null,
                path)
        {
            this.Line = line;
            this.Destination = destination;
            this.DepartureStop = departureStop;
            this.ArrivalStop = arrivalStop;
            this.IntermediateStops = intermediateStops;
            this.Message = message;
        }

        public DateTime GetDepartureTime()
		{
			 DateTime? departureTime = this.DepartureStop.GetDepartureTime();

			if (!departureTime.HasValue)
				throw new ArgumentNullException("DepartureTime");

			return departureTime.Value;
		}

		public bool IsDepartureTimePredicted()
		{
			return this.DepartureStop.IsDepartureTimePredicted();
		}

		public TimeSpan? GetDepartureDelay()
		{
			return this.DepartureStop.GetDepartureDelay();
		}

		public string GetDeparturePosition()
		{
			return this.DepartureStop.GetDeparturePosition();
		}

		public bool IsDeparturePositionPredicted()
		{
			return this.DepartureStop.IsDeparturePositionPredicted();
		}

		public DateTime GetArrivalTime()
		{
			 DateTime? arrivalTime = ArrivalStop.GetArrivalTime();

			if (arrivalTime == null)
				throw new ArgumentNullException("ArrivalTime");

			return arrivalTime.Value;
		}

		public bool IsArrivalTimePredicted()
		{
			return this.ArrivalStop.IsArrivalTimePredicted();
		}

		public TimeSpan? GetArrivalDelay()
		{
			return this.ArrivalStop.GetArrivalDelay();
		}

		public string GetArrivalPosition()
		{
			return this.ArrivalStop.GetArrivalPosition();
		}

		public bool IsArrivalPositionPredicted()
		{
			return this.ArrivalStop.IsArrivalPositionPredicted();
		}

		public override string ToString()
		{
            StringBuilder builder = new StringBuilder(this.GetType().Name + "[");
            builder.Append("line=").Append(Line);
			if (Destination != null)
			{
                builder.Append(",");
                builder.Append("destination=").Append(Destination.ToDebugString());
			}
            builder.Append(",");
            builder.Append("departure=").Append(DepartureStop);
            builder.Append(",");
            builder.Append("arrival=").Append(ArrivalStop);
            builder.Append("]");
            return builder.ToString();
		}
	}
}